﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel;
using ListenMusic.User_Comment_ServiceReference;
using ListenMusic.Models.Reposistory.User;
using ListenMusic.Comment_ServiceReference;
namespace ListenMusic.Controllers.User
{
    public class User_CommentController : ApplicationController
    {
        //
        // GET: /Comment/
        private User_CommentReposistory _comment;
        public User_CommentController()
        {
            _comment = new User_CommentReposistory();
        }
        [Authorize(Roles = "websiteuser")]
        public JsonResult Post(string songID,string album_ID,string comment,int typeID,string returnURL)
        {
            Response.ContentType = "application/ajax";
            try
            {
                if (comment.Length > 10)
                {
                    
                    if (Session["user"] == null)
                    {
                        return Json(false);
                    }
                    else
                    {
                        Comment c = new Comment();
                        if (songID == null && album_ID == null)
                        {
                            return Json(false);

                        }
                        if (songID != null && album_ID != null)
                        {
                            return Json(false);

                        }
                        if (songID != null)
                        {
                            c.SongID = Convert.ToInt32(songID);
                            c.AlbumID = null;
                        }
                        if (songID == null)
                        {
                            c.AlbumID = Convert.ToInt32(album_ID);
                            c.SongID = null;
                        }
                        string[] array_info_user = (string[])Session["user"];
                        c.DateComment = DateTime.Now;
                        c.TypeID = Convert.ToInt32(typeID);
                        c.CommentContent = comment;
                        c.UserName = array_info_user[0];
                        
                        bool res = _comment.PostComment(c);
                        if (res)
                        {
                            return Json(true);
                        }
                        else
                        {
                            return Json(false);

                        }
                    }
                }
                else
                {
                    return Json("Nội dung bình luận có tối thiểu 10 ký tự");
                }
            }
            catch (FaultException ex)
            {
                return Json(false);
               // return View("~/Views/Shared/_Error.cshtml");
                throw ex;
            }
          
        }
       
    }
}
